package com.example.administrator.mywidget;

import android.app.Activity;
import android.content.res.Resources;
import android.gesture.Gesture;
import android.support.annotation.IntRange;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

import java.util.List;

public class MainActivity extends Activity {
    private MyGestureListener myGestureListener;
    private Button bozBtn;
    private View view;
    private ViewFlipper vf;
    private GestureDetector gestureDetector;
    private int MIN_MOVE=200;//最小移动距离
    private MainActivity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity = MainActivity.this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = findViewById(R.id.layout1);
        bozBtn = view.findViewById(R.id.bozBtn);
        vf = findViewById(R.id.viewFlipper);
        bozBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vf.startFlipping();
            }
        });
        myGestureListener = new MyGestureListener();
        gestureDetector = new GestureDetector(this,myGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private class MyGestureListener extends GestureDetector.SimpleOnGestureListener{
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            StringBuffer buffer = new StringBuffer();
            buffer.append("e1:("+e1.getX()+","+e1.getY()+")");
            buffer.append("e2:("+e2.getX()+","+e2.getY()+")");
            System.out.println(buffer);

            double len=Math.pow(Math.pow(e1.getX()-e2.getX(),2)+Math.pow(e1.getY()-e2.getY(),2),1/2);
            System.out.println(len);
            if(len>MIN_MOVE){
                System.out.println("Big");
                if(e1.getX()-e2.getX()>0){
                    //<<
                    vf.setInAnimation(activity,R.anim.left_in);
                    vf.setOutAnimation(activity,R.anim.left_out);
                    vf.showNext();
                }else{
                    //>>
                    vf.setInAnimation(activity,R.anim.right_in);
                    vf.setOutAnimation(activity,R.anim.right_out);
                    vf.showPrevious();
                }
                return true;
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }
}
